package cn.yq.aio.ui.main;

import androidx.core.content.ContextCompat;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import cn.yq.aio.R;
import cn.yq.aio.base.BaseActivity;
import cn.yq.aio.data.MySp;
import cn.yq.aio.databinding.ActivityMainBinding;
import cn.yq.aio.ui.LoginActivity;
import cn.yq.aio.ui.main.chengfen.ChengFenFragment;
import cn.yq.aio.ui.main.shengao.ShenGaoFragment;
import cn.yq.aio.ui.main.tiwen.TiWenFragment;
import cn.yq.aio.ui.main.xindian.XinDianFragment;
import cn.yq.aio.ui.main.xueYa.XueYaFragment;
import cn.yq.aio.ui.main.xuetang.XueTangFragment;
import cn.yq.aio.ui.main.xueyang.XueYangFragment;

/**
 * @AUTHOR YangLu
 * @Time 2021-03-25
 * @todo 首页
 */
public class MainActivity extends BaseActivity implements MainLeftFragment.OnFunctionClick {

    public static void toMe(Context context) {
        Intent it = new Intent(context, MainActivity.class);
        context.startActivity(it);
    }

    ActivityMainBinding binding;

    ShenGaoFragment shenGaoFragment;
    final String tagShenGao = "tagShenGao";

    XinDianFragment xinDianFragment;
    final String tagXinDian = "tagXinDian";

    TiWenFragment tiWenFragment;
    final String tagTiWen = "tagTiWen";

    XueYaFragment xueYaFragment;
    final String tagXueYa = "tagXueYa";

    XueTangFragment xueTangFragment;
    final String tagXueTang = "tagXueTang";

    XueYangFragment xueYangFragment;
    final String tagXueYang = "tagXueYang";

    ChengFenFragment chengFenFragment;
    final String tagChengFen = "tagChengFen";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!MySp.getInstance().isLogin()) {
            LoginActivity.toMe(this);
            finish();
            return;
        }

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.topMain.nameTopTitle.setText("测量页");
        binding.topMain.rightTopTitle.setText("历史数据");
        binding.topMain.rightTopTitle.setTextColor(ContextCompat.getColor(this, R.color.tv_auto));
        binding.topMain.rightTopTitle.setBackgroundResource(R.drawable.history_data_main);
        binding.topMain.rightTopTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        MainLeftFragment leftFragment = MainLeftFragment.newInstance();
        getSupportFragmentManager().beginTransaction().replace(R.id.left_main, leftFragment, "left").commit();

        clickShenGao();
    }

    //身体体重
    @Override
    public void clickShenGao() {
        shenGaoFragment = (ShenGaoFragment) getSupportFragmentManager().findFragmentByTag(tagShenGao);
        if (shenGaoFragment == null) {
            shenGaoFragment = ShenGaoFragment.newInstance();
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.right_main_act, shenGaoFragment, tagShenGao).commit();
    }

    //心电
    @Override
    public void clickXinDian() {
        xinDianFragment = (XinDianFragment) getSupportFragmentManager().findFragmentByTag(tagXinDian);
        if (xinDianFragment == null) {
            xinDianFragment = XinDianFragment.newInstance();
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.right_main_act, xinDianFragment, tagXinDian).commit();
    }

    //体温
    @Override
    public void clickTiWen() {
        tiWenFragment = (TiWenFragment) getSupportFragmentManager().findFragmentByTag(tagTiWen);
        if (tiWenFragment == null) {
            tiWenFragment = TiWenFragment.newInstance();
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.right_main_act, tiWenFragment, tagTiWen).commit();
    }

    //血压
    @Override
    public void clickXueYa() {
        xueYaFragment = (XueYaFragment) getSupportFragmentManager().findFragmentByTag(tagXueYa);
        if (xueYaFragment == null) {
            xueYaFragment = XueYaFragment.newInstance();
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.right_main_act, xueYaFragment, tagXueYa).commit();
    }

    //血糖
    @Override
    public void clickXueTang() {
        xueTangFragment = (XueTangFragment) getSupportFragmentManager().findFragmentByTag(tagXueTang);
        if (xueTangFragment == null) {
            xueTangFragment = XueTangFragment.newInstance();
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.right_main_act, xueTangFragment, tagXueTang).commit();
    }

    //血氧
    @Override
    public void clickXueYang() {
        xueYangFragment = (XueYangFragment) getSupportFragmentManager().findFragmentByTag(tagXueYang);
        if (xueYangFragment == null) {
            xueYangFragment = XueYangFragment.newInstance();
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.right_main_act, xueYangFragment, tagXueYang).commit();
    }

    //人体成分
    @Override
    public void clickChengFen() {
        chengFenFragment = (ChengFenFragment) getSupportFragmentManager().findFragmentByTag(tagChengFen);
        if (chengFenFragment == null) {
            chengFenFragment = ChengFenFragment.newInstance();
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.right_main_act, chengFenFragment, tagChengFen).commit();
    }
}
